#include <vector>
using namespace std;
struct TreeNode {
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode()
      : val(0), left(nullptr), right(nullptr){} * TreeNode(int x)
      : val(x),
        left(nullptr), right(nullptr) {}
  TreeNode(int x, TreeNode *left, TreeNode *right)
      : val(x), left(left), right(right) {}
};

void dfs(const vector<int> &nums, TreeNode *&root, int left, int right) {
  if (left > right) {
    return;
  }
  int mid = (left + right + 1) / 2;
  root = new TreeNode(nums[mid]);
  dfs(nums, root->left, left, mid - 1);
  dfs(nums, root->right, mid + 1, right);
}

TreeNode *sortedArrayToBST(vector<int> &nums) {
  if (nums.size() == 0) {
    return nullptr;
  }
  TreeNode *root = nullptr;
  dfs(nums, root, 0, nums.size() - 1);
  return root;
}